package ch.ethz.fcl.metrobuzz.controller.gui.popup;

import java.awt.Canvas;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.MouseListener;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.render.techniques.projection.Projection;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.spatialindex.object3d.Point3D;
import ch.ethz.fcl.metrobuzz.tools.MBGL;

public class MBPopupMenu extends PopupMenu {
	private static final long serialVersionUID = 1L;

	public static String exit = "Exit";
	public static String zoomCenter = "Set Zoom Center";
	public static String cancleCenter = "Cancle Zoom Center";
	public static String temporalPattern = "Temporal View";
	public static String detail = "Detail View";
	public static String nodetail = "No Detail View";

	private static int mouseX, mouseY;
	private static boolean updateCentroid;
	private static Point2D centroid;
	private static Point3D centerWC;

	public static boolean centerSelected = false;
	
	public static boolean DetailView = false;	
	public static boolean updateMenu = true;
	
	public static boolean showTemporalPattern = false;

	private static float shiftX, shiftY;

	public MBPopupMenu(Canvas canvas) {
		if (MBPopupMenu.updateMenu) {
			for (MouseListener ml : canvas.getMouseListeners())
				if (ml instanceof PopupMenuMouseListener)
					canvas.removeMouseListener(ml);
			if (centroid == null)
				centroid = new Point2D(0, 0);

			PopupMenuActionListener al = new PopupMenuActionListener(this);
			PopupMenuMouseListener ml = new PopupMenuMouseListener(this);
			MenuItem menuItem;

			if (!DetailView) {
				add(menuItem = new MenuItem(detail));
				menuItem.addActionListener(al);
				addSeparator();
			} else {
				add(menuItem = new MenuItem(nodetail));
				menuItem.addActionListener(al);
				addSeparator();
			}
			add(menuItem = new MenuItem(temporalPattern));
			menuItem.addActionListener(al);
			addSeparator();

			add(menuItem = new MenuItem(exit));
			menuItem.addActionListener(al);

			canvas.add(this);
			canvas.addMouseListener(ml);

			updateMenu = false;
		}
	}

	public void setMouse(int x, int y) {
		mouseX = x;
		mouseY = y;
	}

	public Point2D getCentroid() {
		return centroid;
	}

	public void updateCentroid(boolean update) {
		updateCentroid = update;
	}

	public void update() {
		Projection proj = new Projection();

		if (updateCentroid) {
			GL11.glTranslatef(shiftX, shiftY, 0);
			Point2D mouse = new Point2D(mouseX, Display.getHeight() - mouseY);
			Point3D wc = proj.getWorldCoord(mouse, 0);
			centroid = new Point2D(wc.getX(), wc.getY());

			GL11.glTranslatef(-shiftX, -shiftY, 0);
			updateCentroid = false;
		}
		if (!centerSelected)
			GL11.glTranslatef(shiftX, shiftY, 0);

		if (centerSelected) {
			Point2D center = new Point2D(Display.getWidth() / 2,
					Display.getHeight() / 2);
			centerWC = proj.getWorldCoord(center, 0);
		}

		if (centerSelected) {
			if (centerWC != null && centroid != null) {
				shiftX = centerWC.getX() - centroid.getX();
				shiftY = centerWC.getY() - centroid.getY();
			}
			GL11.glTranslatef(shiftX, shiftY, 0);

			GL11.glPointSize(5.0f);
			GL11.glColor4f(0.0f, 1.0f, 0, 1);
			GL11.glBegin(GL11.GL_POINTS);
			MBGL.render(getCentroid());
			GL11.glEnd();
		}

	}
}
